package com.zklh.leetcode.interviewTop150;

/**
 * 跳跃游戏I算法实现, 这里使用贪心算法
 *
 * @Author tongnan
 * @Date 2024/5/11
 * @Version 1.0
 */
public class Solution55 {

    public boolean canJump(int[] nums) {
        int farthest = nums[0];
        int current = 0;
        while (farthest < nums.length - 1) {
            int next = 0;
            if (nums[current] == 0) {
                return false;
            }
            for (int i = 1; i <= nums[current]; i++) {
                if (farthest <= current + i + nums[current + i]) {
                    next = current + i;
                    farthest = current + i + nums[current + i];
                }
            }
            current = next;
        }
        return true;
    }

    public static void main(String[] args) {
        Solution55 solution55 = new Solution55();
        int[] nums = {3,2,1,0,4};
        System.out.println(solution55.canJump(nums));
    }
}